package countDown;

import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by brossdong on 2018/1/23.
 * 同时进行
 */
public class UseCyclicBarrier {

    static class Runner implements Runnable {

        private CyclicBarrier cyclicBarrier;
        private String name;

        public Runner (CyclicBarrier cyclicBarrier,String name) {
            this.cyclicBarrier = cyclicBarrier;
            this.name = name;
        }

        public void run() {
            try {
                Thread.sleep(1000 * (new Random().nextInt(5)));
                System.out.println("准备OK:" + name);
                cyclicBarrier.await();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(name + "GO!");
        }
    }

    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
        ExecutorService executor = Executors.newFixedThreadPool(3);

        executor.submit(new Runner(cyclicBarrier,"管儿子"));
        executor.submit(new Runner(cyclicBarrier,"包儿子"));
        executor.submit(new Runner(cyclicBarrier,"孙儿子"));
    }

}
